package it.trekker.dao;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class ExcursionCoordinate implements Serializable {
	
	protected int id;
	protected GPSCoordinate coordinate;
	protected Date timestamp;
	protected Excursion excursion;
	
	public ExcursionCoordinate() {
		coordinate = new GPSCoordinate();
	}
	
	public ExcursionCoordinate(ExcursionCoordinate coord) {
		this.id = -1;
		this.coordinate = new GPSCoordinate(coord.getCoordinate());
		this.timestamp = coord.getTimestamp();
	}
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public GPSCoordinate getCoordinate() {
		return coordinate;
	}

	public void setCoordinate(GPSCoordinate coordinate) {
		this.coordinate = coordinate;
	}

	public Date getTimestamp() {
		return timestamp;
	}
	
	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}
	
	@ManyToOne(
			cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH},
			fetch=FetchType.LAZY
	)
	public Excursion getExcursion() {
		return excursion;
	}

	public void setExcursion(Excursion excursion) {
		this.excursion = excursion;
	}

	@Override
	public String toString() {
		return String.format("id: %d - %s - time: %s", 
				id, coordinate, timestamp);
	}
	
	@Override
	public boolean equals(Object o) {
		if(o == null)
			return false;
		if(this == o)
			return true;
		if(!(o instanceof ExcursionCoordinate))
			return false;
		
		ExcursionCoordinate other = (ExcursionCoordinate) o;
		
		if(this.coordinate != other.coordinate)
			return false;
		if(!this.timestamp.equals(other.timestamp))
			return false;
		
		return true;
	}
}
